package com.xinyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinyu.domain.EquipmentInfo;
import com.xinyu.service.IEquipmentInfoService;
import com.xinyu.utils.JWTUtils;
import com.xinyu.vo.BaseResult;
import com.xinyu.vo.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author: XinYu
 * @since 2025-02-17
 */
@RestController
@RequestMapping("/equipment-info")
public class EquipmentInfoController {

    @Autowired
    private IEquipmentInfoService equipmentInfoService;

    @Autowired
    private HttpServletRequest request;

    @GetMapping("/queryAll")
    public ResponseEntity<BaseResult> queryAll() {
        // 方式1：使用QueryWrapper添加查询条件
        QueryWrapper<EquipmentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0); // 只查询未删除的数据

        List<EquipmentInfo> list = equipmentInfoService.list(queryWrapper);
        return ResponseEntity.ok(BaseResult.ok(list));
    }

    @GetMapping("queryByUserId/{userId}")
    public ResponseEntity<BaseResult> queryByUserId(@PathVariable Integer userId){
        List<EquipmentInfo> list = equipmentInfoService.queryByUserId(userId);
        return ResponseEntity.ok(BaseResult.ok(list));
    }

    @GetMapping("/visible")
    public ResponseEntity<BaseResult> listVisibleMachines() {
        Integer userId = JWTUtils.getUserIdFromToken(request.getHeader("Authorization"));
        List<EquipmentInfo> list = equipmentInfoService.listVisibleMachines(userId);
        return ResponseEntity.ok(BaseResult.ok(list));
    }

    /**
     * 分页查询 EquipmentInfo
     */
    @PostMapping("/list")
    public ResponseEntity<BaseResult> list(@RequestBody(required = false) PageRequest params) {
        PageRequest pageRequest = params == null ? new PageRequest() : params;
        Page<EquipmentInfo> page = new Page<>(pageRequest.getSafePageNum(), pageRequest.getSafePageSize());
        IPage<EquipmentInfo> result = equipmentInfoService.selectEquipmentInfoPage(page);
        return ResponseEntity.ok(BaseResult.ok(result));
    }

    /**
     * 新增 EquipmentInfo
     */
    @PostMapping("/save")
    public ResponseEntity<BaseResult> save(@RequestBody EquipmentInfo equipmentInfo) {
        equipmentInfoService.save(equipmentInfo);
        return ResponseEntity.ok(BaseResult.ok());
    }

    /**
     * 修改 EquipmentInfo
     */
    @PostMapping("/update")
    public ResponseEntity<BaseResult> update(@RequestBody EquipmentInfo equipmentInfo) {
        equipmentInfoService.updateById(equipmentInfo);
        return ResponseEntity.ok(BaseResult.ok());
    }

    /**
     * 删除 EquipmentInfo
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<BaseResult> delete(@PathVariable Integer id) {
        EquipmentInfo info = equipmentInfoService.getById(id);
        info.setIsDelete(1);
        equipmentInfoService.updateById(info);
        return ResponseEntity.ok(BaseResult.ok());
    }

}

